package com.example.demo.mock.user;

import com.example.demo.service.mock.user.MockUserService;
import com.example.demo.table.User;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/mock_user")
@Api(value = "用户信息接口")
public class MockUserController {
    @Autowired
    private MockUserService mockUserService;

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ApiOperation(value = "删除用户", notes = "根据id来删除用户")
    @JsonView(value = String.class)
    public String delete(int id) {
        int result = mockUserService.delete(id);
        if (result >= 1) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "更新用户信息", notes = "根据id来更新用户信息")
    @JsonView(value = String.class)
    public String update(User user) {
        int result = mockUserService.Update(user);
        if (result >= 1) {
            return "修改成功";
        } else {
            return "修改失败";
        }

    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "插入新的用户", notes = "插入新的用户")
    @JsonView(Integer.class)
    public int insert(User user) {
        return mockUserService.insertUser(user);
    }

    @RequestMapping(value = "/ListUser", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查找用户", notes = "查找出所有用户")
    @JsonView(User.class)
    public List<User> ListUser() {
        return mockUserService.ListUser();
    }


    @RequestMapping(value = "/ListUserByName", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查找用户", notes = "根据用户名找出用户集合")
    @JsonView(User.class)
    public List<User> ListUserByName(String name) {
        return mockUserService.findUserByName(name);
    }
}
